package org.ilovecode.display {
	import fl.transitions.Tween;
	import fl.transitions.easing.Regular;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.ilovecode.display.faq.FaqItem;
	import org.ilovecode.events.FaqEvent;
	import org.ilovecode.mvc.View;
	import org.ilovecode.screen.ScreenGroup;
	
	/**
	 * Original provided by Mario Nikolaus.
	 * 
	 * @author Vjekoslav Ratkajec
	 */
	public class FAQContent extends View {
		
		//Number of questions and answers defined as elements on stage.
		private var NUM_ELEMENTS:uint = 35;
		
		protected var faqItems:Array;
		
		/**
		 * Create instance of FaqScreenContent.
		 */
		public function FAQContent() {
			super();
		}
		
		override protected function construct():void
		{
			super.construct();
			
			faqItems = new Array();
			
			var screenXML:XML = controller.getScreenXML("faq", ScreenGroup.APARTMENTS);
			var fi:FaqItem;
			for each (var xml:XML in screenXML.descendants("item")) 
			{
				fi = new FaqItem(int(xml.@id), String(xml.question), String(xml.answer), "faqItem" + xml.@id);
				faqItems.push(fi);
				this.addChild(fi);
			}
		}
		
		override protected function initUI():void 
		{
			super.initUI();
			
			var newY:Number = 70;
			for each (var fi:FaqItem in faqItems) 
			{
				fi.y = newY;
				newY += fi.minHeight;
				this.addChild(fi);
			}
		}	
		
		override protected function addListeners():void
		{
			super.addListeners();
			model.addEventListener(FaqEvent.SWITCH_ITEM, update);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			model.removeEventListener(FaqEvent.SWITCH_ITEM, update);
		}
		
		override protected function update(event:Event):void
		{
			switch(event.type)
			{
				case FaqEvent.SWITCH_ITEM:
					handle_SwitchItem(FaqEvent(event).offset);
					break;
				default:
					super.update(event);
					break;
			}
		}
		
		protected function handle_SwitchItem(offset:Number):void
		{
			this.y -= offset;
			this.dispatchEvent(new Event(Event.RESIZE));
		}		
		
	}

}